# -*- coding: utf-8 -*-
import requests
import  json
import logging


case = {'name': '获取统一的鉴权码token', 'title': '验证正常返回首页数据', 'requests': {'method': 'post', 'url': 'http://ic.test.imotorlinx.cn/api/v1/user/login', 'data': {'username': '13333333333', 'password': '123456'}, 'headers': {'Content-Type': 'application/json'}}, 'validate': 'None'}

#生成日志对象
logger = logging.getLogger(__name__)

class RequestsUtil:

    # def __init__(self,case):
    #     # pass
    #     self.case = case
    # 类变量：通过类名访问
    session = requests.session()

    def send_requests(self,base_url,method,url,headers,data=None,**kwargs):
        url = base_url + url
        method = str(method).lower()
        #打印日志：请求方式
        logger.info("请求方式：%s" % method)
        #打印日志：请求url
        logger.info("请求url：%s" % url)
        #打印日志：请求头
        logger.info("请求头：%s" % headers)
        #打印日志：请求参数
        logger.info("请求参数：%s" % data)
        if method == 'get':
            rep = RequestsUtil.session.request(method,url=url,headers=headers,params=data,**kwargs)
        elif method == 'post':
            #如果是post，先把它的数据转换成字符串，再通过data去传参
            data = json.dumps(data)
            rep = RequestsUtil.session.request(method,url=url,headers=headers,data=data,**kwargs)
        elif method == 'delete':
            rep = RequestsUtil.session.request(method,url=url,headers=headers,params=data,**kwargs)
        elif method == 'put':
            rep = RequestsUtil.session.request(method,url=url,headers=headers,data=data,**kwargs)
        #校验yaml格式
        # new_case = verify_yaml(self.case)
        # print(new_case.requests)
        # re = RequestsUtil.session.request(**new_case.requests)
        #打印日志：响应参数
        logger.info("响应正文：%s"%rep.text)
        # print(rep.text)
        # print(rep.json())
        return rep


if __name__ == '__main__':
    headers = {'Content-Type':'application/json'}
    a = RequestsUtil().send_requests('post','http://ic.test.imotorlinx.cn/api/v1/user/login',{'username': '13333333333', 'password': '123456'},headers=headers)
    print(a.text)

